<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class MY_Model extends CI_Model{
		function __construct() {
        	parent::__construct();
    	}
    	public function insert_data($data){                         // insert 1 row cho bang
        $this->db->db_debug = FALSE;
        $this->db->insert($this->getTable(), $data);
        if ($this->db->affected_rows() > 0){   //? false:true;
            return;
        }  else {
            $e = $this->db->_error_message();//false;//"Không được phép xóa!";
            return $e;
        }
    }
    public function insert_data_option($colum, $value){         // insert 1 row cho bang voi so cot tuy chon
        $str = "";
        for($i = 0; $i < count($colum); $i++){
            if($i < $colum - 1){
                $str += "'".$colum[$i]."' => ".$value[$i].",";
            }  else {
                $str += "'".$colum[$i]."' => ".$value[$i];
            }
        }
        $_array = array($str);
        $this->db->set($_array);
        $this->db->insert($this->getTable());
        return ($this->db->affected_rows() != 1)? false:true;
    }
    public function update_data($data, $condition){
        if(!empty($condition) && is_array($condition)){
            foreach ($condition as $key => $value) {
                if(is_numeric($key)){
                    $this->db->where($value);
                }  else {
                    $this->db->where($key, $value);
                }
            }
            $this->db->db_debug = FALSE;
            $this->db->update($this->getTable(), $data);
            if($this->db->affected_rows() > 0){
                return;
            } else {
                return $this->db->_error_message();
            }
        }
    }
    public function delete_data($condition){
        if(!empty($condition) && is_array($condition)){
            foreach ($condition as $key => $value) {
                if(is_numeric($key)){
                    $this->db->where($value);
                }  else {
                    $this->db->where($key, $value);
                }
            }
            $this->db->delete($this->getTable());
            if($this->db->affected_rows() > 0){
                return true;
            } else {
                return $this->db->_error_message();
            }
        }
    }
    public function delete($key, $condition){       // xoa nhieu row co cung dieu kien
//        $this->db->where_in($key, $condition);
//        $this->db->delete($this->getTable());
        $str ='';
        $n = count($condition);
        for($i = 0; $i < $n; $i++ ){
            if($i < $n - 1){
                $str .= '"'.$condition[$i].'",';
            }  else {
                $str .= '"'.$condition[$i].'"';
            }
        }
        $sql = 'delete from '.$this->getTable().' where '.$key.' in ('.$str.')';
        $this->db->db_debug = FALSE;
        $q = $this->db->query($sql);
        if(!$q){
            $e = $this->db->_error_message();//false;//"Không được phép xóa!";
            return $e;
        }
        return $q;
    }
	}

?>